package com.zjc.code.algorithm.my;

import java.util.Arrays;

/**
 * @Author: JinChen Zhu
 * @CreateTime: 2024-07-24  10:45
 */
//@SuppressWarnings("all")
public class Code03_CopyArray {
    public static void main(String[] args) {
        int[] arr = generateArr(30, 20);
        int[] arrCopy = copyArray(arr);
        Code02_Sort.printArr(arr);
        Code02_Sort.printArr(arrCopy);
        Arrays.sort(arrCopy);
        Code02_Sort.bubbleSort(arr);
        Code02_Sort.printArr(arr);
        Code02_Sort.printArr(arrCopy);
        if (isEqual(arr, arrCopy)) {
            System.out.println("测试成功");
        } else {
            System.out.println("测试失败");
        }
    }

    public static int[] generateArr(int maxSize, int maxValue) {
        if (maxSize < 1) {
            throw new IllegalArgumentException("maxSize must be greater than 0.");
        }
        if (maxValue < 0) {
            throw new IllegalArgumentException("maxValue must be greater than or equal to 0.");
        }

        int arraySize = 1 + (int) (maxSize * Math.random()); // 确保数组大小至少为1
        int[] arr = new int[arraySize];
        for (int i = 0; i < arr.length; i++) {
            // 生成-maxValue到maxValue之间的随机数
            int positiveRandom = (int) (Math.random() * (maxValue + 1));
            int negativeRandom = (int) (Math.random() * (maxValue + 1));
            arr[i] = positiveRandom - negativeRandom;
        }
        return arr;
    }

    public static boolean isEqual(int[] arr1, int[] arr2) {
        if (arr1 == null || arr2 == null) {
            throw new IllegalArgumentException("The array must not be null.");
        }
        if (arr1.length != arr2.length) {
            return false;
        }
        for (int i = 0; i < arr1.length; i++) {
            if (arr1[i] != arr2[i]) {
                return false;
            }
        }
        return true;
    }

    public static int[] copyArray(int[] arr) {
        int[] copyArr = new int[arr.length];
        System.arraycopy(arr, 0, copyArr, 0, arr.length);
        return copyArr;
    }
}
